package com.fypg.module.medical.service.registerorder;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.registerorder.vo.*;
import com.fypg.module.medical.dal.dataobject.registerorder.RegisterOrderDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.registerorder.RegisterOrderMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 挂号订单信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RegisterOrderServiceImpl implements RegisterOrderService {

    @Resource
    private RegisterOrderMapper registerOrderMapper;

    @Override
    public String createRegisterOrder(RegisterOrderSaveReqVO createReqVO) {
        // 插入
        RegisterOrderDO registerOrder = BeanUtils.toBean(createReqVO, RegisterOrderDO.class);
        registerOrderMapper.insert(registerOrder);
        // 返回
        return registerOrder.getId();
    }

    @Override
    public void updateRegisterOrder(RegisterOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateRegisterOrderExists(updateReqVO.getId());
        // 更新
        RegisterOrderDO updateObj = BeanUtils.toBean(updateReqVO, RegisterOrderDO.class);
        registerOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteRegisterOrder(String id) {
        // 校验存在
        validateRegisterOrderExists(id);
        // 删除
        registerOrderMapper.deleteById(id);
    }

    @Override
        public void deleteRegisterOrderListByIds(List<String> ids) {
        // 校验存在
        validateRegisterOrderExists(ids);
        // 删除
        registerOrderMapper.deleteByIds(ids);
        }

    private void validateRegisterOrderExists(List<String> ids) {
        List<RegisterOrderDO> list = registerOrderMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(REGISTER_ORDER_NOT_EXISTS);
        }
    }

    private void validateRegisterOrderExists(String id) {
        if (registerOrderMapper.selectById(id) == null) {
            throw exception(REGISTER_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public RegisterOrderDO getRegisterOrder(String id) {
        return registerOrderMapper.selectById(id);
    }

    @Override
    public PageResult<RegisterOrderDO> getRegisterOrderPage(RegisterOrderPageReqVO pageReqVO) {
        return registerOrderMapper.selectPage(pageReqVO);
    }

}